﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class ProgrameController : BaseController
    {
        private readonly IMapper _mapper;
        private readonly ProgrameInfoApp _programeInfoApp;

        public ProgrameController(IMapper mapper, ProgrameInfoApp programeInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._mapper = mapper;
            this._programeInfoApp = programeInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public  ActionResult ProgrameInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }

        /// <summary>
        /// 程序配置列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetProgrameAllListAsync()
        {
            var where = new QueryProgrameInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.ProgrameName = Request.Form["name"].ToString();
            where.ProgrameCode = Request.Form["code"].ToString();
            var proList = await _programeInfoApp.GetPagedProgrameInfoAsync(where);
            var proDtoList = _mapper.Map<List<ProgrameInfo>, List< ProgrameInfoDto>> (proList.Result);
            var pageData = new TableData<ProgrameInfoDto>(Draw, proList.Total, proList.Total, proDtoList);
            //var disEqList = await _equipmentApp.GetALlDispatchEquipmentAsync();
            //var pageData = new TableData<DispatchEquipmentInfoDto>(Draw, disEqList.Count, disEqList.Count, disEqList);
            //await AddTracesInfo(5, 0, "获取程序配置列表");
            return Json(pageData);
        }

        public async Task<ActionResult> ProgrameAddOrEditAsync(long id)
        {
            var programeDto = new ProgrameInfoDto();
            if (id > 0)
            {
                var result = await _programeInfoApp.GetProgrameInfoByIdAsync(id);
                programeDto = _mapper.Map<ProgrameInfo, ProgrameInfoDto>(result);
            }
            return View(programeDto);
        }


        public async Task<JsonResult> OperateProgrameInfoAsync()
        {
            var pro_infoDto = new ProgrameInfoDto();
            pro_infoDto.ProgrameCode = Request.Form["code"].ToString();
            pro_infoDto.ProgrameName = Request.Form["name"].ToString();
            pro_infoDto.ID = Request.Form["id"].ToInt64();

            var proInfo = _mapper.Map<ProgrameInfoDto, ProgrameInfo>(pro_infoDto);
            bool result;
            string msg;
            try
            {
                if (pro_infoDto.ID == 0)
                {
                    result = await _programeInfoApp.CreateProgrameInfoAsync(proInfo);
                    await AddTracesInfo(2, 0, "新增程序配置");
                }
                else
                {
                    result = await _programeInfoApp.ModifyProgrameInfoAsync(proInfo);
                    await AddTracesInfo(3, 0, "修改程序配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        public async Task<JsonResult> DelProgrameInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _programeInfoApp.DelectProgrameInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除程序配置");
            return CreateJsonGetResult(result, msg);
        }
    }
}
